<?php
class MyZendX_Form_Element_Uploadify extends Zend_Form_Element_File
{
	protected $_uploadifyOptions = array();
	
	/**
	 * @param string|array $value
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function setValue($value)
	{
		$this->_value = $value;
		$this->getTransferAdapter()->addFiles($this->getName(), $value);
		return $this;
	}
	
	/**
	 * @param string|array $value
	 * @param array $context OPTIONAL
	 * @return boolean
	 */
	public function isValid($value, $context = null)
	{
		$this->setValue($value);
		
		if ($this->isRequired()
			&& $this->autoInsertNotEmptyValidator()
			&& !$this->getValidator('NotEmpty'))
		{
			$validators = $this->getValidators();
			$index = array_search('Zend_Validate_File_Upload', $validators);
			if (false !== $index) {
				unset($validators[$index]);
			}
			//$fileExists = new Zend_Validate_File_Exists($this->getTransferAdapter()->getBackendUploadFolder());
			//array_unshift($validators, $fileExists);
			$notEmpty   = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true);
			array_unshift($validators, $notEmpty);
			$this->setValidators($validators);
		}

		return parent::isValid($value, $context);
	}
	
	/**
	 * @return MyZendX_File_Transfer_Adapter_Uploadify
	 */
	public function getTransferAdapter()
	{
        if (null === $this->_adapter) {
            $this->setTransferAdapter(new MyZendX_File_Transfer_Adapter_Uploadify());
        }
        return $this->_adapter;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Zend/Form/Element/Zend_Form_Element_File::loadDefaultDecorators()
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function loadDefaultDecorators()
	{
		if ($this->loadDefaultDecoratorsIsDisabled()) {
			return $this;
		}

		$decorators = $this->getDecorators();
		if (empty($decorators)) {
			$this	->addDecorator(new MyZendX_Form_Decorator_Uploadify())
					->addDecorator('File')
					->addDecorator('Errors')
					->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
					->addDecorator('HtmlTag', array('tag' => 'dd'))
					->addDecorator('Label', array('tag' => 'dt'));
		}
		return $this;
	}
	
	/**
	 * @param array $options
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function setUploadifyOptions(array $options)
	{
		foreach($options as $key => $value) {
			$method = 'setUploadify' . ucfirst($key);
			if (method_exists($this, $method)) {
				$this->$method($value);
			}
			else {
				$this->_uploadifyOptions[$key] = $value;
			}
		}
		return $this;
	}
	
	/**
	 * @return array
	 */
	public function getUploadifyOptions()
	{
		return $this->_uploadifyOptions;
	}
	
	/**
	 * @param string $url
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function setUploadifyUploadUrl($url)
	{
		$this->_uploadifyOptions['script'] = $url;
		return $this;
	}

	/**
	 * @return string
	 */
	public function getUploadifyUploadUrl()
	{
		return $this->_uploadifyOptions['script'];
	}

	/**
	 * @param boolean $flag
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function setUploadifyPreview($flag)
	{
		$this->_uploadifyOptions['preview'] = $flag;
		return $this;
	}
	
	/**
	 * @return boolean
	 */
	public function getUploadifyPreview()
	{
		return $this->_uploadifyOptions['preview'];
	}
	
	/**
	 * @param string $url
	 * @return MyZendX_Form_Element_Uploadify
	 */
	public function setUploadifyPreviewUrl($url)
	{
		$this->_uploadifyOptions['previewUrl'] = $url;
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function getUploadifyPreviewUrl()
	{
		return $this->_uploadifyOptions['previewUrl'];
	}
}